{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "86973d44",
   "metadata": {},
   "outputs": [],
   "source": [
    "from VecSim import *\n",
    "import numpy as np\n",
    "\n",
    "dim = 128\n",
    "num_elements = 1000\n",
    "\n",
    "# Create a brute force index for vectors of 128 floats. Use 'L2' as the distance metric\n",
    "bf_params = BFParams()\n",
    "bf_params.initialCapacity = num_elements\n",
    "bf_params.blockSize = num_elements\n",
    "bf_index = BFIndex(bf_params, VecSimType_FLOAT32, dim, VecSimMetric_L2)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "03ff62d4",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Add 1M random vectors to the index\n",
    "data = np.float32(np.random.random((num_elements, dim)))\n",
    "vectors = []\n",
    "\n",
    "for i, vector in enumerate(data):\n",
    "    bf_index.add_vector(vector, i)\n",
    "    vectors.append((i, vector))\n",
    "\n",
    "print(f'Index size: {bf_index.index_size()}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bc831b57",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Create a random query vector\n",
    "query_data = np.float32(np.random.random((1, dim)))\n",
    "\n",
    "# Create batch iterator for this query vector\n",
    "batch_iterator = bf_index.create_batch_iterator(query_data)\n",
    "returned_results_num = 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a3c3fbee",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Get the next best results\n",
    "batch_size = 100\n",
    "labels, distances = batch_iterator.get_next_results(batch_size, BY_SCORE)\n",
    "\n",
    "print (f'Results in rank {returned_results_num}-{returned_results_num+len(labels[0])} are: \\n')\n",
    "print (f'labels: {labels}')\n",
    "print (f'scores: {distances}')\n",
    "\n",
    "returned_results_num += len(labels[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7925ecf6",
   "metadata": {},
   "outputs": [],
   "source": [
    "import time\n",
    "\n",
    "# Run batches until depleted\n",
    "batch_size = 15\n",
    "start = time.time()\n",
    "while(batch_iterator.has_next()):\n",
    "    labels, distances = batch_iterator.get_next_results(batch_size, BY_ID)\n",
    "    returned_results_num += len(labels[0])\n",
    "\n",
    "print(f'Total results returned: {returned_results_num}\\n')\n",
    "print(f'Total search time: {time.time() - start}')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda env:vsim] *",
   "language": "python",
   "name": "conda-env-vsim-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
